package me.shiqiang.im.service;

import com.aliyun.openservices.ons.api.Action;
import com.aliyun.push20160801.Client;
import com.aliyun.push20160801.models.PushNoticeToiOSRequest;
import com.aliyun.push20160801.models.PushRequest;
import com.aliyun.push20160801.models.PushResponse;
import com.aliyun.teautil.models.RuntimeOptions;
import com.tst.common.exception.MicroServiceException;
import com.tst.common.exception.ServiceException;
import com.tst.common.returnobj.ReturnObjCode;
import me.shiqiang.im.enums.MessageCType;
import me.shiqiang.im.msg.MsgBody;
import me.shiqiang.im.msg.NewMsg;
import me.shiqiang.im.util.AppNotifyUtil;
import me.shiqiang.im.utils.JsonUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

@Service
public class NotifyService {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    Client client;

    @Value("${apns.env}")
    String env;

    @Autowired
    GroupService groupService;

    public void send(NewMsg newMsg){
        MsgBody msgBody = newMsg.getMsgBody();
        Long userId = newMsg.getUserId();
        AppNotifyUtil appNotifyUtil = new AppNotifyUtil();
//        PushNoticeToiOSRequest pushNoticeToiOSRequest = new PushNoticeToiOSRequest()
//                .setAppKey(334761549L)
//                .setTarget("ACCOUNT")
//                .setTargetValue(userId.toString())
//                .setApnsEnv(env);

        PushRequest pushRequest = new PushRequest();
        pushRequest.setAppKey(334761549L)
                .setTarget("ACCOUNT")
                .setTargetValue(userId.toString())
                .setPushType("NOTICE")
                .setDeviceType("iOS")
                .setIOSBadgeAutoIncrement(true)
                .setIOSApnsEnv(env);

        RuntimeOptions runtime = new RuntimeOptions();

        if (msgBody.getcType() == Integer.parseInt(MessageCType.GROUP)) {
            if (msgBody.getAt().equals(1)) {
//                pushNoticeToiOSRequest.setTitle("群消息");
//                pushNoticeToiOSRequest.setBody(String.format("%s在群聊中@了你",msgBody.getFromName()));
                pushRequest.setTitle("群消息");
                pushRequest.setBody(String.format("%s在群聊中@了你",msgBody.getFromName()));
            }else {
                if (groupService.isNoDisturb(newMsg.getUserId(), msgBody.getTo(), 2)) {
                    logger.info("已设置免打扰");
                    return;
                }
//                pushNoticeToiOSRequest.setTitle("群消息");
//                pushNoticeToiOSRequest.setBody("您收到了一条新消息");
                pushRequest.setTitle("群消息");
                pushRequest.setBody("您收到了一条新消息");
            }

            //带来的todo: 客户端屏蔽功能，回到最久远未读消息

        } else {
//            pushNoticeToiOSRequest.setTitle(msgBody.getFromName());
//            pushNoticeToiOSRequest.setBody("您收到了一条新消息");
            pushRequest.setTitle(msgBody.getFromName());
            pushRequest.setBody("您收到了一条新消息");
        }
        try {
            logger.info(JsonUtils.getInstance().toJson(pushRequest));
//            com.aliyun.push20160801.models.PushNoticeToiOSResponse resp = client.pushNoticeToiOSWithOptions(pushNoticeToiOSRequest, runtime);
            PushResponse resp = client.pushWithOptions(pushRequest, runtime);
            if (resp.getStatusCode() != 200) {
                logger.warn("APP通知微服务服务返回失败,response:{}", resp.getBody().toString());
                throw new ServiceException(resp.getBody().toString());
            }
        } catch (Exception e) {
            logger.error(String.format("消息通知失败,msg:%s", e));
        }
    }

}
